package com.ouc.zykt.interceptor;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ouc.openplatform.pampas.SessionManager;
import com.ouc.pampas.ClusterHttpServletRequestWrapper;
import com.ouc.pampas.ClusterHttpSessionWrapper;
import com.ouc.pampas.SessionSecurityConstants;
/**
 * 用户登陆拦截器
 */
public class LoginContextInterceptor extends HandlerInterceptorAdapter {

	//不被拦截器拦截的url
	private List<String> noLoginAuthUrlList = new ArrayList<String>();
	public List<String> getNoLoginAuthUrlList() {
		return noLoginAuthUrlList;
	}
	public void setNoLoginAuthUrlList(List<String> noLoginAuthUrlList) {
		this.noLoginAuthUrlList = noLoginAuthUrlList;
	}

	/**
	 * 预处理
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		
		if(!isLogin(request)){
			response.sendRedirect("/login"); 
			return false;
		}
	
		return super.preHandle(request, response, handler);
	}

	/**
	 * 拦截
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 返回方法拦截
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	/**
	 * 判断是否登录
	 */
	protected boolean isLogin(HttpServletRequest httpServletRequest) {		
		String currentUrl = httpServletRequest.getRequestURI();
		for(String url : noLoginAuthUrlList){
			if(currentUrl.startsWith(url)){
				return true;
			}
		}
		
		Cookie[] cookies = httpServletRequest.getCookies();
	    String sid = "";
	    //从cookie中找到sessionKey,并赋值给sid
	    if (cookies != null) {
	      for (int i = 0; i < cookies.length; i++)
	      {
	        Cookie cookie = cookies[i];
	        if (cookie.getName().equals("zykt_sessionKey"))
	        {
	          sid = cookie.getValue();
	          break;
	        }
	      }
		}
	    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(httpServletRequest.getServletContext());
	    SessionManager sessionService = ((SessionManager)applicationContext.getBean("sessionService", SessionManager.class));
		ClusterHttpServletRequestWrapper clusterHttpServletRequest = new ClusterHttpServletRequestWrapper(httpServletRequest.getServletContext(), httpServletRequest, sid, sessionService);
		ClusterHttpSessionWrapper clusterHttpSessionWrapper = (ClusterHttpSessionWrapper)clusterHttpServletRequest.getSession();
	    //System.out.println("222session创建时间"+clusterHttpSessionWrapper.getCreationTime());
	    long now=System.currentTimeMillis();
	    //System.out.println("现在时间"+now);
	    if(now-clusterHttpSessionWrapper.getCreationTime()>3000000) {
	    	System.out.println("清除session");
	    	clusterHttpSessionWrapper.invalidate();
	    	return false;
	    }
	    HttpSession httpSession = httpServletRequest.getSession();
		String userName = (String) httpSession.getAttribute(SessionSecurityConstants.KEY_USER_NAME);
		//System.out.println("session中username的值为"+userName);
		if (userName == null) {
			return false;
		}
		return true;
	}

}
